package com.yanqu.road.server.cmd.activity.stronghold.wild;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.StrongholdWarUserData;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.*;

@Cmd(code = GameProtocol.S_STRONGHOLD_WAR_WILD_MOVE, desc = "野外据点指定移动")
public class StronghWildMoveCmd extends AbstractCommand {
    private int wildId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.MoveToStrongholdReqMsg req = StrongholdWarProto.MoveToStrongholdReqMsg.parseFrom(packet.getBytes());
        wildId = req.getId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //判断体力
        //先扣体力，跨服验证后有需要再补
        StrongholdWarProto.CrossMoveToStrongholdReqMsg.Builder req = StrongholdWarProto.CrossMoveToStrongholdReqMsg.newBuilder();
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            sendPlayer(player);
            return;
        }

        if (!StrongholdActivityMgr.isWildTime()) {
            ret = GameErrorCode.E_ACTIVITY_NOT_OPEN;
            sendPlayer(player);
            return;
        }

        StrongholdWarUserData warData = player.getModule(StrongholdModule.class).getStrongholdWarData(activityInfo.getActivityId());
        player.getModule(StrongholdModule.class).calcPower(warData);
        if (warData.getPower() < StrongholdActivityMgr.getMovePower()) {
            ret = GameErrorCode.E_STRONGHOLD_POWER_NOT_ENOUHT;
            sendPlayer(player);
            return;
        }
        req.setActivityId(activityInfo.getActivityId());
        req.setId(wildId);
        req.setPlayerData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        player.sendUnionActivityGroupPacket(Protocol.C_CROSS_STRONGHOLD_WAR_WILD_MOVE, req, req.getActivityId());
    }

    private void sendPlayer(GamePlayer player) {
        StrongholdWarProto.CrossMoveToStrongholdRespMsg.Builder resp = StrongholdWarProto.CrossMoveToStrongholdRespMsg.newBuilder();
        resp.setRet(ret);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_WILD_MOVE,resp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
